package com.security.door.controller.accessManage;

import com.security.door.model.vo.baseDictTreeVo;
import com.security.door.comm.TreeBuilder;
import com.security.door.core.wrapper.WrapMapper;
import com.security.door.core.wrapper.Wrapper;
import com.security.door.model.domain.baseDatadict;
import com.security.door.model.vo.deviceInfoVo;
import com.security.door.model.vo.treeViewVo;
import com.security.door.service.IbaseDatadictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;


@RestController
@Api(value = "WEB - baseDatadictController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class baseDatadictController {

    @Autowired
    HttpSession session;
    @Autowired
    HttpServletRequest httpServletRequest;
    @Autowired
    IbaseDatadictService ibaseDatadictService;

    @GetMapping("/base/selectDatadict")
    @ApiOperation(value = "查询常量数据列表", notes = "查询常量数据列表")
    public Wrapper selectDatadict() {
        List<baseDictTreeVo> treeData = ibaseDatadictService.selectTreeList();
        var list = getTree(treeData,0);
        return WrapMapper.ok(list);
    }

    private List<baseDictTreeVo> getTree(List<baseDictTreeVo> list, Integer parentId) {
        List<baseDictTreeVo> listChildrens = null;
        if (parentId == null) {
            listChildrens = list.stream().filter(x -> x.getParentmodelkey() == parentId).collect(Collectors.toList());
        } else {
            list.removeIf(x -> x.getParentmodelkey() == null);
            listChildrens = list.stream().filter(x -> x.getParentmodelkey().equals(parentId)).collect(Collectors.toList());
        }
        List<baseDictTreeVo> listVo = new ArrayList<baseDictTreeVo>();
        for (var children : listChildrens) {
            children.setId(children.getModelvalue());
            children.setLabel(children.getModelname());
            var tempList = getTree(list, children.getModelkey());
            if (tempList.size() > 0)
                children.setChildren(tempList);
            listVo.add(children);
        }
        return listVo;
    }

    @GetMapping("/base/selectAllDict")
    @ApiOperation(value = "查询常量数据列表", notes = "查询常量数据列表")
    public Wrapper selectAllDict() {
        List<baseDatadict> list = ibaseDatadictService.selectAll();
        return WrapMapper.ok(list);
    }

    @PostMapping("/base/insertDatadict")
    @ApiOperation(value = "新增常量数据", notes = "新增常量数据")
    public Wrapper InsertDatadict(@RequestBody baseDatadict entity) {
        if ("".equals(entity.getGuid()) || entity.getGuid() == null) {
            entity.setGuid(UUID.randomUUID().toString());
            int maxkey = ibaseDatadictService.getMaxModelkey() + 1;
            entity.setModelkey(maxkey);
            entity.setModelvalue(maxkey);
            entity.setCreatetime(new Date());
            ibaseDatadictService.save(entity);
        } else {
            ibaseDatadictService.update(entity);
        }
        return WrapMapper.ok("操作成功");
    }

    @GetMapping("/base/getDatadict/{guid}")
    @ApiOperation(value = "根据GUID查询数据", notes = "根据GUID查询数据")
    public Wrapper getDatadict(@PathVariable("guid") String guid) {
        treeViewVo result = ibaseDatadictService.getDatadictByID(guid);
        return WrapMapper.ok(result);
    }

    @GetMapping("/base/deleteDatadict/{guid}")
    @ApiOperation(value = "根据GUID删除数据", notes = "根据GUID删除数据")
    public Wrapper deleteDatadict(@PathVariable("guid") String guid) {
        ibaseDatadictService.deleteByKey(guid);
        return WrapMapper.ok("操作成功");
    }
}
